package com.smart_campus.sys.controller;

import com.smart_campus.sys.common.ActiveUser;
import com.smart_campus.sys.common.Constant;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.domain.Feedback;
import com.smart_campus.sys.domain.User;
import com.smart_campus.sys.service.FeedbackService;
import com.smart_campus.sys.vo.FeedbackVo;
import com.smart_campus.sys.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Alfred
 * @time: 2020/6/18 14:29
 * 问题反馈控制类
 */
@RestController
@RequestMapping("feed")
public class FeedBackController {

    @Autowired
    private FeedbackService feedbackService;

    /**
     * @return 查询全部
     */
    @RequestMapping("queryAllFeedback")
    public DataGridView queryAllFeedback(FeedbackVo feedbackVo){

        return feedbackService.queryAllFeedback(feedbackVo);
    }

    /**
     * 修改
     * @param feedback
     * @return
     */
    @RequestMapping("updateFeedback")
    public ResultObj updateFeedback(Feedback feedback){
        try {
            this.feedbackService.updateByPrimaryKeySelective(feedback);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 添加建议
     * @param feedback
     * @return
     */
    @RequestMapping("addFeedback")
    public ResultObj addFeedback(Feedback feedback){
        try {
            Subject subject= SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            User user = activeUser.getUser();
            feedback.setCreatetime(new Date());
            feedback.setIsrenting(Constant.isrenting);
            Integer queryMax = this.feedbackService.queryMax();
            feedback.setOrdernum(queryMax);
            feedback.setUser_Id(user.getUserId());
            Integer idDept = this.feedbackService.queryByIdDept(user.getUserId());
            feedback.setDept_Id(idDept);
            this.feedbackService.insertSelective(feedback);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 删除意见
     * @param id
     * @return
     */
    @RequestMapping("deleteFeedback")
    public ResultObj deleteFeedback(Integer id){
        try {
            this.feedbackService.deleteByPrimaryKey(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除意见
     * @param ids
     * @return
     */
    @RequestMapping("deleteBatchFeedback")
    public ResultObj deleteBatchFeedback(Integer ids[]){
        try {
            for (Integer id: ids) {
                this.feedbackService.deleteByPrimaryKey(id);
            }
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
